/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import util.filter.Predicate;

/**
 *
 * @author damfercer
 */
@Entity
@Table(name = "search_relocating_info_requeriment_and_operator")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "SearchRelocatingInfoRequerimentAndOperator.findAll", query = "SELECT s FROM SearchRelocatingInfoRequerimentAndOperator s"),
    @NamedQuery(name = "SearchRelocatingInfoRequerimentAndOperator.findByIdSearchRelocatingInfoRequerimentAndOperator", query = "SELECT s FROM SearchRelocatingInfoRequerimentAndOperator s WHERE s.idSearchRelocatingInfoRequerimentAndOperator = :idSearchRelocatingInfoRequerimentAndOperator")})
public class SearchRelocatingInfoRequerimentAndOperator implements Serializable,Predicate<Candidate> {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID_SEARCH_RELOCATING_INFO_REQUERIMENT_AND_OPERATOR")
    private Integer idSearchRelocatingInfoRequerimentAndOperator;
    @JoinColumn(name = "COUNTRY", referencedColumnName = "ID_COUNTRY")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Country country;
    @JoinColumn(name = "SEARCH", referencedColumnName = "ID_SEARCH")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Search search;
    @JoinColumn(name = "PROVINCE", referencedColumnName = "ID_PROVINCE")
    @ManyToOne(fetch = FetchType.LAZY)
    private Province province;

    public SearchRelocatingInfoRequerimentAndOperator() {
    }

    public SearchRelocatingInfoRequerimentAndOperator(Integer idSearchRelocatingInfoRequerimentAndOperator) {
        this.idSearchRelocatingInfoRequerimentAndOperator = idSearchRelocatingInfoRequerimentAndOperator;
    }

    public Integer getIdSearchRelocatingInfoRequerimentAndOperator() {
        return idSearchRelocatingInfoRequerimentAndOperator;
    }

    public void setIdSearchRelocatingInfoRequerimentAndOperator(Integer idSearchRelocatingInfoRequerimentAndOperator) {
        this.idSearchRelocatingInfoRequerimentAndOperator = idSearchRelocatingInfoRequerimentAndOperator;
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    public Search getSearch() {
        return search;
    }

    public void setSearch(Search search) {
        this.search = search;
    }

    public Province getProvince() {
        return province;
    }

    public void setProvince(Province province) {
        this.province = province;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idSearchRelocatingInfoRequerimentAndOperator != null ? idSearchRelocatingInfoRequerimentAndOperator.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
                boolean countryRes=false;
        boolean provinceRes=false;
        if (!(object instanceof SearchRelocatingInfoRequerimentAndOperator)) {
            return false;
        }
        SearchRelocatingInfoRequerimentAndOperator other = (SearchRelocatingInfoRequerimentAndOperator) object;
        
        //Sector test
        if(this.getCountry()==null && other.getCountry()==null){
            countryRes=true;
        }
        else if(this.getCountry()!=null && other.getCountry()!=null){
            countryRes=this.getCountry().equals(other.getCountry());
        }
        //MinimumEducationalLevel test
        if(this.getProvince()==null && other.getProvince()==null){
            provinceRes=true;
        }
        else if(this.getProvince()!=null && other.getProvince()!=null){
            provinceRes=this.getProvince().equals(other.getProvince());
        }
        
        Boolean res=countryRes && provinceRes;
        return res;
    }

    @Override
    public String toString() {
        return "entities.SearchRelocatingInfoRequerimentAndOperator[ idSearchRelocatingInfoRequerimentAndOperator=" + idSearchRelocatingInfoRequerimentAndOperator + " ]";
    }

    @Override
    public boolean test(Candidate c) {
        boolean pass=false;
        if(getCountry()!=null && getProvince()!=null){
            pass=getCountry().equals(c.getCountry()) && getProvince().equals(c.getProvince());
            if(pass){
                return pass;
            }
        }
        if(c.getCandidateRelocatingInfoList()!=null){
            for(CandidateRelocatingInfo cri: c.getCandidateRelocatingInfoList()){
                if(getCountry()!=null && getProvince()==null && cri.getCountry()!=null && cri.getProvince()==null){
                    pass=getCountry().equals(cri.getCountry());
                }
                else if(getCountry()!=null && getProvince()!=null && cri.getCountry()!=null){
                    pass=getCountry().equals(cri.getCountry()) && (cri.getProvince()==null || getProvince().equals(cri.getProvince()));
                }
                if(pass){
                    break;
                }
            }
        }
        return pass;
    }
    
}
